package com.example.sqlite.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.example.sqlite.adapters.MyListAdapter;
import com.example.sqlite.dao.ContactsDAO;
import com.example.sqlite.models.Contact;
import com.example.sqlite.models.HeaderObject;

public class ListExpandableActivity extends Activity {

	private ContactsDAO dao;
	private ArrayList<HeaderObject> objList = new ArrayList<HeaderObject>();
	private List<Contact> contacts;
	private MyListAdapter listAdapter;
	private ExpandableListView myList;
	private CheckBox checkBoxTest;

	private final OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
			if ( isChecked )
				expandAll();
			//
			else
				collapseAll();
		}
	};

	@Override
	public void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_list_expandable );

		dao = new ContactsDAO( this );
		dao.open();

		myList = (ExpandableListView) findViewById( R.id.myList );
		checkBoxTest = (CheckBox) findViewById( R.id.checkb );

		checkBoxTest.setOnCheckedChangeListener( listener2 );
		myList.setOnItemLongClickListener( onLongClickListenerGroup );
	}

	private void loadObjects() {
		objList = new ArrayList<HeaderObject>();
		for ( Contact contact : contacts ) {
			objList.add( new HeaderObject( contact ) );
		}
	}

	@Override
	protected void onResume() {
		dao.open();
		super.onResume();
		contacts = dao.getAll();
		loadObjects();

		listAdapter = new MyListAdapter( ListExpandableActivity.this, objList );
		myList.setAdapter( listAdapter );
	}

	private void expandAll() {
		int count = listAdapter.getGroupCount();
		for ( int i = 0; i < count; i++ ) {
			myList.expandGroup( i );
		}
	}

	private void collapseAll() {
		int count = listAdapter.getGroupCount();
		for ( int i = 0; i < count; i++ ) {
			myList.collapseGroup( i );
		}
	}

	private final OnItemLongClickListener onLongClickListenerGroup = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick( AdapterView<?> arg0, View arg1, int arg2, long arg3 ) {
			switch ( arg0.getId() ) {
				case R.id.myList:
					HeaderObject headerInfo = objList.get( arg2 );
					Intent intent = new Intent( ListExpandableActivity.this, ShowContactActivity.class );
					intent.putExtra( "id", headerInfo.getObjId() );
					startActivity( intent );
					break;
			}
			return false;
		}
	};

	@SuppressWarnings( "unused" )
	private final OnChildClickListener myListItemClicked = new OnChildClickListener() {

		@Override
		public boolean onChildClick( ExpandableListView parent, View v, int groupPosition, int childPosition, long id ) {
			/*
			 * HeaderObject headerInfo = objList.get(groupPosition); // get the
			 * child info DetailInfo detailInfo =
			 * headerInfo.getAtributosList().get( childPosition); // display it
			 * or do something with it Toast.makeText( getBaseContext(),
			 * "Clicked on Detail " + headerInfo.getObjName() + "/" +
			 * detailInfo.getValue(), Toast.LENGTH_LONG).show();
			 */
			return false;
		}

	};

	@SuppressWarnings( "unused" )
	private final OnGroupClickListener myListGroupClicked = new OnGroupClickListener() {

		@Override
		public boolean onGroupClick( ExpandableListView parent, View v, int groupPosition, long id ) {
			/* HeaderObject headerInfo = objList.get(groupPosition); */
			/*
			 * Toast.makeText(getBaseContext(), "Child on Header " +
			 * headerInfo.getObjName(), Toast.LENGTH_LONG).show();
			 */
			return false;
		}
	};

	@Override
	public boolean onCreateOptionsMenu( Menu menu ) {
		getMenuInflater().inflate( R.menu.activity_main, menu );
		return true;
	}

	@Override
	public boolean onOptionsItemSelected( MenuItem item ) {
		if ( item.getItemId() == R.id.add_contact ) {
			Intent intent = new Intent( ListExpandableActivity.this, AddContactActivity.class );
			startActivity( intent );
		}
		return super.onOptionsItemSelected( item );
	}

}